import { getTableHeadBtns, getTableAction } from '@supplier/utils/btnsUtils';
import BtnRender from '@/components/JSXRender/BtnRender';
import BasicTableForm from '@/components/supplier/BasicForm';
import BasicTable from '@/components/supplier/BasicTable';
import ExportDialog from '@supplier/components/ExportDialog';
import ConfigTableColumnsDialog from '@/pages/supplier/components/configTableColumnsDialog';
import SelectSignType from '../components/SelectSignType';
import SelectStandardOrNot from '../components/SelectStandardOrNot';
import {
  renewContractService,
} from '@/api/supplier/management/supplierContract';
import {
  getListColumnSettings,
} from '@/api/supplier/components'
import Setting from '@/setting'

export default {
  components: {
    BasicTableForm,
    BasicTable,
    ExportDialog,
    BtnRender,
    ConfigTableColumnsDialog,
    SelectSignType,
    SelectStandardOrNot
  },
  data () {
    return {
      fieldSettingVisible: false,
      signTypeVisible: false,
      standardVisible: false,
      selectId: []
    }
  },
  created () {
    this.getColSettings();
  },
  methods: {
    // 表头操作项
    renderBtnsBar (h) {
      const { list, selection } = this.tableInfo;
      const btnsBar = [
        { label: '批量维护计划签订类型', method: this.handleBatchSelectSignType, disabled: !selection.length, },
        { label: '批量维护合同是否达标', method: this.handleBatchSelectStandardOrNot, disabled: !selection.length },
        { label: '字段显示设置', method: this.handleConfigTableColumns, hasAuth: false, position: 'end' },
      ];
      if (this.supplierExportConfig.contract) {
        btnsBar.push({
          label: '导出',
          method: this.handleExport,
          disabled: !list.length
        })
      }
      const finalBars = btnsBar.filter(item => item).map(item => ({
        ...item,
        type: 'primary',
        size: 'mini',
        disabled: item.disabled ?? this.hasNoSelection,
        hasAuth: item.hasAuth ?? true
      }));
      return getTableHeadBtns(h, finalBars, this.showBtn);
    },
    // 表列操作项
    renderTableBtnAction (h, { row }) {
      const actionList = [];
      // signingCondition 新签/续签签订情况  3：已在登记表签订
      if (row.signingCondition != 3) {
        // 计划签订类型不存在 展示计划签订类型按钮 || 不签合同审批状态 10:待审批 20:已审批 30:驳回 40:撤回
        if (!row.signingType || row.notContractApprovalStatus == 30) {
          actionList.push({ label: '计划签订类型', method: this.handleSelectSignType, hasAuth: true })
        }
        // 是否签订合同 == 是 && !是否达标
        // 合同签订不存在 计划签订类型 1:签订合同 2:不签订合同 
        if (row.signingType == 1 && !row.contractStandard) {
          actionList.push({ label: '合同是否达标', method: this.handleSelectStandardOrNot, hasAuth: true })
        }
        // 合同是否达标 1:是 2:否 && 合同签订未成功
        if (row.contractStandard && row.signSuccessOrNot != 1) {
          actionList.push({ label: '签订合同', method: this.handleSignContract, hasAuth: true })
        }
      }
      return getTableAction({ h, row, actionList, showBtn: this.showBtn });
    },
    // 查询条件整理
    initSearchParams () {
      const [contractStartTime, contractEndTime] = this.formData.contractTime || [];
      return _.omit({
        ...this.formData,
        contractStartTime,
        contractEndTime,
      }, 'contractTime')
    },
    // 计划签订类型
    handleSelectSignType (row) {
      this.signTypeVisible = true;
      this.selectId = [row.id]
    },
    // 批量维护签订类型
    handleBatchSelectSignType () {
      if (this.tableInfo.selection.some(item => item.signingType && item.notContractApprovalStatus != 30 || item.signingCondition == 3)) {
        return this.$message.warning('请勾选计划签订类型为空或不签合同审批状态为驳回状态的数据且新签/续签签订情况不为已在登记表签订');
      }
      this.selectId = this.tableInfo.selection.map(i => i.id)
      this.signTypeVisible = true;
    },
    // 是否达标
    handleSelectStandardOrNot (row) {
      this.standardVisible = true;
      this.selectId = [row.id]
    },
    // 批量维护是否达标
    handleBatchSelectStandardOrNot () {
      if (this.tableInfo.selection.some(item =>
        item.signingType != 1
        || item.contractStandard
        || item.signingCondition == 3
      )) {
        return this.$message.warning('请勾选计划签订类型为签订合同且合同是否达标为空的数据且新签/续签签订情况不为已在登记表签订');
      }
      this.selectId = this.tableInfo.selection.map(i => i.id)
      this.standardVisible = true;
    },
    // 签订合同
    async handleSignContract (data) {
      const { supplierId } = data
      const res = await renewContractService({ supplierId, operateType: 1, singleFlag: 0 });
      if (res) {
        const contractUrl = `${Setting.SUPPLIER_CONTRACT_HOST}${res.result.url}`
        window.open(contractUrl);
      }
    },
    // 获取列表配置
    async getColSettings () {
      const { status, message, result = [] } = await getListColumnSettings({ menuId: this.menuId });
      if (status === '0') {
        this.setTableColumns(result)
      } else {
        this.$message.error(message)
      }
    },
    // 应用列表配置
    setTableColumns (data) {
      const arr = data.filter(i => i.isShow).map(i => {
        return {
          title: i.label,
          key: i.prop,
          width: i.width
        }
      })
      this.tableInfo.columns = arr
    },
    // 列表字段配置
    handleConfigTableColumns () {
      this.fieldSettingVisible = true;
    },
    // 字段显示设置弹窗关闭
    handleConfigTableColumnsDialogClose () {
      this.fieldSettingVisible = false;
    },
    // 提交字段显示设置
    hanldSubmitTableColumnsConfig (data) {
      this.setTableColumns(data)
      this.fieldSettingVisible = false;
    },
  }
}
